package game.components;

import game.ball.SoccerBall;
import game.players.SoccerPlayer;
import game.players.SoccerPlayerCPUMate;
import game.players.SoccerPlayerCPUOpponent;
import game.players.SoccerPlayerUser;
import game.players.states.CPUMateWithoutBall;
import game.soccerField.Arc;
import game.soccerField.SoccerField;

import java.awt.Point;
import java.util.ArrayList;
import java.util.List;

import com.uqbar.vainilla.GameScene;
import com.uqbar.vainilla.appearances.Sprite;

public class MatchScene extends GameScene {
	
	private List<SoccerPlayer> players   = new ArrayList<SoccerPlayer>();
	private List<SoccerPlayer> teammates = new ArrayList<SoccerPlayer>();
	private List<SoccerPlayer> opponents = new ArrayList<SoccerPlayer>();
	private List<Arc> arcs               = new ArrayList<Arc>();
	private SoccerBall soccerBall;
	private Camera camera;
	private SoccerField field;
	private SoccerPlayer userPlayer;
	private UserPlayerMark userPlayerMark;

	public MatchScene(Camera camera) {
		this.setUpArcs();
		this.setUpTeammates();
		this.setUpOpponents();
		this.setUpField();
		
		this.setSoccerBall(new SoccerBall(400, 300));
		this.setCamera(camera);
		this.addComponents(this.getCamera(), this.getField(), this.getSoccerBall());
		this.addComponents(this.getSoccerPlayers());
		this.addComponents(this.getArcs());
		this.setPlayersBall();
		this.setUserPlayerMark(new UserPlayerMark(this.userPlayer));
	}

	public void setPlayersBall() {
		for(SoccerPlayer p:this.players)
			p.setBall(this.soccerBall);
	}

	private void setUpArcs() {
		this.getArcs().add(new Arc(1650,145));
		
		Arc arc = new Arc(-925,145);
		Sprite sprite = ((Sprite)arc.getAppearance()).flipHorizontally();
		arc.setAppearance(sprite);
		this.getArcs().add(arc);
	}

	private void setUpOpponents() {
		this.getOpponents().add(new SoccerPlayerCPUOpponent(1500, 250)); //arquero
		this.getOpponents().add(new SoccerPlayerCPUOpponent(1000, 100));
		this.getOpponents().add(new SoccerPlayerCPUOpponent(1000, 400));
		this.getOpponents().add(new SoccerPlayerCPUOpponent(600, 0));
		this.getOpponents().add(new SoccerPlayerCPUOpponent(600, 500));
		
		this.setOppenentsArc();
		
		this.getPlayers().addAll(this.getOpponents());
	}

	public void setOppenentsArc() {
		for(SoccerPlayer o : this.getOpponents())
			o.setRivalArc(this.getArcs().get(1));
	}

	private void setUpTeammates() {
		this.userPlayer = new SoccerPlayerUser(300, 250);
		this.getTeammates().add(this.userPlayer);
		this.getTeammates().add(new SoccerPlayerCPUMate(-700, 250)); //arquero
		this.getTeammates().add(new SoccerPlayerCPUMate(-200, 0));
		this.getTeammates().add(new SoccerPlayerCPUMate(-200, 500));
		this.getTeammates().add(new SoccerPlayerCPUMate(200, 100));
		
		this.setTeammatesArc();
		
		this.getPlayers().addAll(this.getTeammates());
	}

	public void setTeammatesArc() {
		for(SoccerPlayer t : this.getTeammates())
			t.setRivalArc(this.getArcs().get(0));
	}

	private void setUpField() {
		SoccerField field = new SoccerField();
		field.move(-1150, -240);
		this.setField(field);
	}
	
	public void moveSprites(double x, double y) {
		this.getField().move(-x, -y);

		for(Arc arc : this.getArcs())
			arc.move(-x, -y);
		
		for (SoccerPlayer player : this.getSoccerPlayers()) {
			if (!player.isWithBall())
				player.move(-x, -y);
		}
	}	
	
	public SoccerPlayer getCloserBallTeammate(SoccerPlayer player) {
		SoccerPlayer closer = null;
		double minDist = 999999;
		for(SoccerPlayer sp : this.teammates){
			if(!sp.equals(player) && this.distToBall(sp) < minDist){
				closer = sp;
				minDist = this.distToBall(sp);
			}				
		}
		
		return closer;
	}	

	private double distToBall(SoccerPlayer sp) {
		return Point.distance(sp.getX(),sp.getY(),this.soccerBall.getX(),this.soccerBall.getY());
	}
	
	public void removeUserPlayerMark() {
		this.userPlayerMark.destroy();
	}

	public SoccerBall getSoccerBall() {
		return soccerBall;
	}

	public void setSoccerBall(SoccerBall soccerBall) {
		this.soccerBall = soccerBall;
	}

	public List<SoccerPlayer> getSoccerPlayers() {
		return players;
	}

	public void setCamera(Camera camera) {
		this.camera = camera;
	}

	public Camera getCamera() {
		return camera;
	}

	public SoccerField getField() {
		return field;
	}

	public void setField(SoccerField field) {
		this.field = field;
	}
	
	public List<SoccerPlayer> getTeammates() {
		return teammates;
	}	
	
	public List<Arc> getArcs() {
		return arcs;
	}

	public List<SoccerPlayer> getOpponents() {
		return opponents;
	}

	public List<SoccerPlayer> getPlayers() {
		return players;
	}
	
	public SoccerPlayer getUserPlayer() {
		return userPlayer;
	}
	
	public void setUserPlayer(SoccerPlayer userPlayer) {
		this.userPlayer.setState(new CPUMateWithoutBall(this.userPlayer));//cambio estado del user player anterior
		this.userPlayer = userPlayer;		
	}
	
	public void setUserPlayerMark(UserPlayerMark userPlayerMark) {
		this.userPlayerMark = userPlayerMark;
		this.addComponent(this.userPlayerMark);
	}


}